package com.eqdd.yiqidian.ui.tongxunlu;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.TeamMemberListActivityCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.GroupMemberListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.mine.UserCardActivity;
import com.eqdd.yiqidian.utils.ImageUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.List;

public class TeamMemberListActivity extends BaseActivity {
    private TeamMemberListActivityCustom dataBinding;
    private String groupid;
    private SlimAdapterEx<GroupMemberListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_team_member_list);
        initTopTitleBar(View.VISIBLE, "群成员");
    }

    @Override
    public void initData() {
        groupid = getIntent().getStringExtra("groupid");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_26, new SlimInjector<GroupMemberListResultBean>() {
            @Override
            public void onInject(GroupMemberListResultBean data, IViewInjector injector) {
                injector.text(R.id.tv_upper_content, data.getUpname())
                        .with(R.id.iv_head, v -> ImageUtil.setCircleImage(data.getIphoto(), (ImageView) v));
            }
        })
                .attachTo(dataBinding.recyclerView);
        ItemClickSupport.addTo(dataBinding.recyclerView)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putString(Config.GUID, slimAdapterEx.getData().get(position).getGuid());
                    startActivity(UserCardActivity.class, bundle);
                });
    }

    @Override
    public void setView() {
        OkGo.<HttpResult<List<GroupMemberListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GROUP_USER_LIST)
                .params("groupid", groupid)
                .execute(new DialogCallBack<HttpResult<List<GroupMemberListResultBean>>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<GroupMemberListResultBean>>> response) {
                        HttpResult<List<GroupMemberListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
